package org.dmikis.task11.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import org.dmikis.task11.PhasePoint;
import org.dmikis.task11.parameter.*;

import java.util.ArrayList;


public class ParametersEditorDialog extends JPanel {
    
    public ParametersEditorDialog() {
	super();
    }
    
    public void showDialog() {
	this.dialog = new JDialog();
	this.dialog.setTitle("Параметры");
	this.dialog.add(this, BorderLayout.CENTER);
	JPanel buttonPanel = new JPanel();
	JButton okButton = new JButton("OK");
	okButton.addActionListener(new ActionListener() {

	    @Override
	    public void actionPerformed(ActionEvent arg0) {
		dialog.setVisible(false);
		isOK = true;
		saveData();
	    }	    
	});
	buttonPanel.add(okButton);
	JButton cancelButton = new JButton("Отмена");
	cancelButton.addActionListener(new ActionListener(){

	    @Override
	    public void actionPerformed(ActionEvent e) {
		dialog.setVisible(false);
		isOK = false;
	    }
	});
	buttonPanel.add(cancelButton);
	this.dialog.add(buttonPanel, BorderLayout.SOUTH);
	this.dialog.setModal(true);
	this.dialog.setVisible(true);
    }
    
    protected void saveData() {
	Component[] components = this.getComponents();
	int parameterIndex;
	for (parameterIndex=0; parameterIndex<this.parametersList.size(); parameterIndex++) {
	    // TODO Fix for multiple parameter types
	    Double newValue = Double.parseDouble(((JTextField) components[2*parameterIndex+1]).getText());
	    ((Parameter<Double>) this.parametersList.get(parameterIndex)).setValue(newValue);
	}
    }
    
    public PhasePoint getNewStartingPoint() {
	return new PhasePoint(Double.parseDouble(this.startPointCoordinate0.getText()), Double.parseDouble(this.startPointCoordinate1.getText()),0.0);
    }
    
    public double getNewError() {
	return Double.parseDouble(this.error.getText());
    }
    
    public void setParametersList(ArrayList<AbstractParameter> parametersList) {
	this.parametersList = parametersList;
	this.removeAll();
	if (this.parametersList != null) {
	    this.setLayout(new GridLayout(this.parametersList.size() + 3, 2));
	    for (AbstractParameter parameter : this.parametersList) {
		this.add(new JLabel(parameter.getName()));
		this.add(new JTextField());
	    }
	}
	this.add(new JLabel(ParametersEditorDialog.START_POINT_LABEL_NAME_0));
	this.add(this.startPointCoordinate0);
	this.add(new JLabel(ParametersEditorDialog.START_POINT_LABEL_NAME_1));
	this.add(this.startPointCoordinate1);
	this.add(new JLabel("Погрешность"));
	this.add(this.error);
    }
    
    public boolean isOK;
    
    protected JTextField startPointCoordinate0 = new JTextField();
    protected JTextField startPointCoordinate1 = new JTextField();
    protected JTextField error = new JTextField();
    
    protected static final String START_POINT_LABEL_NAME_0 = "Начальная скорость";
    protected static final String START_POINT_LABEL_NAME_1 = "Начальный угол";

    protected JDialog dialog;
    protected ArrayList<AbstractParameter> parametersList = null;
    
    private static final long serialVersionUID = 1L;
}
